package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.UserLoginService;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserLoginServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements UserLoginService {
    @Override
    public ResponseResult login(AdUserDto dto) {

        System.out.println("dto = " + dto.getName());
        //参数校验
        if(StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"用户名或密码不能为空");
        }
        //根据名称模糊查询
        LambdaQueryWrapper<AdUser> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<AdUser> wrapper = queryWrapper.eq(AdUser::getName, dto.getName());
        AdUser adUser = getOne(wrapper);
//        //讲查询结果存放到list中
//        List<AdUser> list = list(queryWrapper);
//        //如果集合不为空并且集合中只有一个对象
//        if(list!=null && list.size()==1){
//            //获取第一个对象
//            AdUser adUser = list.get(0);
//            //对密码md5手动加盐加密
//            String pwd = DigestUtils.md5DigestAsHex((dto.getPassword()+ adUser.getSalt()).getBytes());
//            //判断数据库中密码是否和获取的密码相同
//            if(adUser.getPassword().equals(pwd)){
//                //获取token返回用户信息
//                HashMap<String, Object> map = Maps.newHashMap();
//                adUser.setPassword("");
//                adUser.setSalt("");
//                map.put("token", AppJwtUtil.getToken(adUser.getId().longValue()));
//                map.put("user",adUser);
//                return ResponseResult.okResult(map);
//            }else {
//                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
//            }
//        }else {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户名不存在");
//        }
       if(adUser==null) {
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
       }
        String pwd = DigestUtils.md5DigestAsHex((dto.getPassword() + adUser.getSalt()).getBytes());
       if(pwd.equals(adUser.getPassword())){
           Map map = new HashMap<>();
           String token = AppJwtUtil.getToken(Long.valueOf(adUser.getId()));
           map.put("token",token);
           adUser.setSalt("");
           adUser.setPassword("");
           map.put("user",adUser);
           return ResponseResult.okResult(map);
       }else {
           return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
       }
    }

}
